package com.example.onion.hxchat;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.superyi.myteam.R;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.util.List;

/**
 * 先前测试环信聊天写的小活动，待删！
 * 提醒自己删之前先删布局文件
 * @author:onion
 * @data: 2020/5/19
 */
public class ChatActivity extends AppCompatActivity implements EMMessageListener {

    private EditText mInputContentView;
    private TextView mContentView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        mInputContentView = findViewById(R.id.ec_edit_message_input);
        mContentView = findViewById(R.id.ec_text_content);

        final String ChatID = "1234";
        findViewById(R.id.ec_btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mInputContentView.getText().toString();
                //创建一条文本消息，content为消息文字内容，toChatUsername为对方用户或者群聊的id，后文皆是如此
                EMMessage message = EMMessage.createTxtSendMessage(content, ChatID);
                //如果是群聊，设置chattype，默认是单聊
                //if (chatType == CHATTYPE_GROUP)
                // message.setChatType(EMMessage.ChatType.GroupChat);
                mContentView.setText(mContentView.getText()+"\n"+content);
                //发送消息
                EMClient.getInstance().chatManager().sendMessage(message);
                message.setMessageStatusCallback(new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        Log.d("application","消息发送成功");
                    }

                    @Override
                    public void onError(int i, String s) {
                        Log.d("application","消息发送失败"+s);
                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        });
    }

    /**
     * 收到消息
     * @param list
     */
    @Override
    public void onMessageReceived(List<EMMessage> list) {
        for (EMMessage message : list){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mContentView.setText(mContentView.getText()+"\n"
                    +((EMTextMessageBody)message.getBody()).getMessage());
                }
            });
        }
    }

    /**
     * 收到透传消息
     * @param list
     */
    @Override
    public void onCmdMessageReceived(List<EMMessage> list) {

    }

    @Override
    public void onMessageRead(List<EMMessage> list) {

    }

    @Override
    public void onMessageDelivered(List<EMMessage> list) {

    }

    @Override
    public void onMessageRecalled(List<EMMessage> list) {

    }

    @Override
    public void onMessageChanged(EMMessage emMessage, Object o) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        //注册消息监听
        EMClient.getInstance().chatManager().addMessageListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消消息监听
        EMClient.getInstance().chatManager().removeMessageListener(this);
    }
}
